<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { propApi } from '@/api/controller';
  import { productTypeArr, counterClassify } from '@/pinia/getter';
  import { ToastSuccess, Toastloading, allNumber } from '@/utils/public';
  import xmUpload from '@/components/Upload/xm-upload.vue';
  const formLabelWidth = '100px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    title:'',
    dialogFormVisible:false,
    disabled:false,
    giftList:[] as Array<any>,
    counterlabelClassify:[] as Array<any>
  })
  interface formQuert {
    id:number
    name:string
    counter_id:number
    counter_cate_id:number
    goods_type:number
    goods_id:number
    order:number
    explain:string
  }
  let form = reactive({} as formQuert)
  const formRules = {
    counter_id: [{ required: true, message: "请选择货架类型", trigger: "blur" }],
    counter_cate_id: [{ required: true, message: "请选择货架标签", trigger: "blur" }],
    goods_type: [{ required: true, message: "请选择礼物类型", trigger: "blur" }],
    goods_id: [{ required: true, message: "请选择货架礼物", trigger: "blur" }],
    order: [{ required: true, message: "请输入货架排序", trigger: "blur" }],
  }
  const getGiftList = async()=>{
    const res = await propApi.getGifts({limit:1000})
    const { data } = res?.data || []
    action.giftList = data 
  }
  const counterCateSList = async(counter_id:number = 0)=>{
    if(!counter_id) return action.counterlabelClassify = []
    const res = await propApi.getCounterCate({ counter_id })
    const { data } = res
    action.counterlabelClassify = data
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const { id } = form
      const loading = Toastloading()
      propApi.setCounterCate(form).then(()=>{
        loading.close()
        ToastSuccess(id?'编辑成功':'新增成功')
        closeDialog()
        $emit('updateList')
      }).catch(()=>{
        loading.close()
      })
    })
  }
  const openDialog = async(query:any = {})=> {
    const { 
      id='',goods_id='',goods_type=1,name='',
      order=10000, explain='',counter_id=1,counter_cate_id='',
    } = query
    Object.assign(form,{
      id,goods_id,goods_type,name,
      order,explain,counter_id,counter_cate_id,
    })
    action.title = id?'编辑货架礼物：':'新增货架礼物'
    const loading = Toastloading()
    await counterCateSList(counter_id)
    if(action.giftList.length == 0) await getGiftList()
    loading.close()
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="5vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="50%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
          <el-form-item :label-width="formLabelWidth" label="货架名称">
            <el-input v-model="form.name" placeholder="请输入货架名称"  />
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="货架类型" prop="counter_id">
            <el-select v-model="form.counter_id" placeholder="请选择货架类型" @change="counterCateSList">
              <el-option
                v-for="item in counterClassify"
                :label="item.label"
                :value="item.type"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="货架标签" prop="counter_cate_id">
            <el-select v-model="form.counter_cate_id" placeholder="请选择货架标签">
              <el-option
                v-for="item in action.counterlabelClassify"
                :label="item.name"
                :value="item.counter_cate_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="礼物类型" prop="goods_type">
            <el-select v-model="form.goods_type" placeholder="请选择礼物类型">
              <el-option label="钻石" :value="1" />
            </el-select>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="货架礼物" prop="goods_id">
            <el-select v-model="form.goods_id" filterable placeholder="请选择货架礼物">
              <el-option
                v-for="item in action.giftList"
                :label="item.name"
                :value="item.gift_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="货架排序" prop="order">
            <el-input v-model="form.order" @input="allNumber(form,'order')" placeholder="请输入货架排序" />
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="货架说明">
            <el-input
              v-model="form.explain"
              :autosize="{ minRows: 3, maxRows: 3}"
              type="textarea"
              placeholder="请输入货物说明"
            />
          </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>